package com.gly091020.touhouLittleMad.util;

import com.gly091020.touhouLittleMad.LittleMadMod;
import net.minecraft.network.chat.Component;
import net.minecraft.world.InteractionHand;
import net.minecraft.world.InteractionResult;
import net.minecraft.world.entity.Entity;
import net.minecraft.world.entity.LivingEntity;
import net.minecraft.world.entity.player.Player;
import net.minecraft.world.item.Item;
import net.minecraft.world.item.ItemStack;
import net.minecraft.world.item.TooltipFlag;
import net.neoforged.bus.api.IEventBus;
import net.neoforged.fml.loading.FMLEnvironment;
import net.neoforged.neoforge.registries.DeferredItem;
import net.neoforged.neoforge.registries.DeferredRegister;
import org.jetbrains.annotations.NotNull;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.function.Supplier;

public class GLYToolsRegistry {
    private static final DeferredRegister.Items ITEMS_REGISTRY = DeferredRegister.createItems(LittleMadMod.ModID);
    public static final Map<String, DeferredItem<Item>> items = new HashMap<>();
    public static void registry(IEventBus bus){
        registry("player_not_die", () -> simpleItem("防死亡", "防止玩家死亡"));
        registry("maid_good_moon", () -> simpleOnUseItem("恢复心情", "快速恢复心情", entity -> {
            if(entity instanceof MaidMadExtraData data){
                data.setMood(0);
            }
        }));

        if(!FMLEnvironment.production){
            ITEMS_REGISTRY.register(bus);
        }
    }
    private static void registry(String name, Supplier<Item> supplier){
        items.put(name, ITEMS_REGISTRY.register(name, supplier));
    }

    public static Item simpleItem(Component name, Component tip){
        return new Item(new Item.Properties()){
            @Override
            public @NotNull Component getName(@NotNull ItemStack stack) {
                return name;
            }

            @Override
            public void appendHoverText(@NotNull ItemStack p_41421_, @NotNull TooltipContext p_339594_, @NotNull List<Component> components, @NotNull TooltipFlag p_41424_) {
                components.add(tip);
            }
        };
    }

    public static Item simpleOnUseItem(Component name, Component tip, Consumer<Entity> onUse){
        return new Item(new Item.Properties()){
            @Override
            public @NotNull Component getName(@NotNull ItemStack stack) {
                return name;
            }

            @Override
            public void appendHoverText(@NotNull ItemStack p_41421_, @NotNull TooltipContext p_339594_, @NotNull List<Component> components, @NotNull TooltipFlag p_41424_) {
                components.add(tip);
            }

            @Override
            public @NotNull InteractionResult interactLivingEntity(@NotNull ItemStack stack, @NotNull Player player, @NotNull LivingEntity interactionTarget, @NotNull InteractionHand usedHand) {
                onUse.accept(interactionTarget);
                return InteractionResult.SUCCESS;
            }
        };
    }

    public static Item simpleItem(String name, String tip){
        return simpleItem(Component.literal(name), Component.literal(tip));
    }

    public static Item simpleOnUseItem(String name, String tip, Consumer<Entity> onUse){
        return simpleOnUseItem(Component.literal(name), Component.literal(tip), onUse);
    }
}
